// ActionScript file
import com.adobe.air.notification.AbstractNotification;
import com.adobe.air.notification.Notification;
import com.adobe.air.notification.NotificationClickedEvent;
import com.adobe.air.notification.Purr;

import flash.desktop.NotificationType;
import flash.events.Event;
import flash.net.URLRequest;  

private var purr:Purr = new Purr(1);
private var bmp: Bitmap = null;
private var soundFactory:Sound = new Sound();
 
public function initPurr():void{
  	var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, iconLoaded);
    loader.load(new URLRequest("/icons/ChatLogo48.png"));	 
 	//Load Purr Sound
	soundFactory.load(new URLRequest("/assets/ding.mp3"));
    purr.notificationSound = soundFactory;
}
 
private function iconLoaded(evt:Event):void{
  	//set Purr Icon
	bmp = Bitmap(Loader(evt.target.loader).content);
	purr.setIcons([bmp.bitmapData]);	
}

public function showNotification(msgt:String,msgx:String):void{
	if(NativeApplication.supportsDockIcon){
        var dock:DockIcon = NativeApplication.nativeApplication.icon as DockIcon;
        dock.bounce(NotificationType.CRITICAL);
    }
  	var n:Notification = new Notification(msgt, msgx, AbstractNotification.TOP_RIGHT, 5, bmp);
  	n.height = 100;
  	n.width = 300;
	n.addEventListener(NotificationClickedEvent.NOTIFICATION_CLICKED_EVENT,
	function(e:NotificationClickedEvent):void
	{
      //After setting the window to visible, make sure that the application is ordered to the front,       
      //else we'll still need to click on the application on the taskbar to make it visible
      stage.nativeWindow.visible = true;
      stage.nativeWindow.orderToFront();
      
      //Clearing the bitmaps array also clears the applcation icon from the systray
      NativeApplication.nativeApplication .icon.bitmaps = [];
	});
    if(SO.data.notify_sound == true){
    	purr.notificationSound = soundFactory;
    }else{
    	purr.notificationSound = null;
    }
    
    if(SO.data.notify_visual == true){
		purr.addNotification(n);		  	
    }
}